﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Net;
using System.IO;

using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace WpfApplicationWithGrid
{

    

    class Communication
    {
        private string url;
        private static Communication com = null;
        private static string token;
        private Communication()
        {
            url = "";
            token = "";
        }

        public static Communication getCommunication()
        {
            if (com == null) com = new Communication();
            return com;
        }
        public void getConfig(string action)
        {
            Parameter param = Parameter.getParameter();

        
            //First set the header           
            param.Token = token;
            param.Action = (action == null) ? "getConfig":"update";
            param.Status = 0;
            jsonConverter jc = jsonConverter.getConverter();

            //parse the object into json string 
            string update = jc.setParameterToJson();

            //make a request to the server by calling process (p is the status code indicating parameter)
            //and then once it responds, set the update json data back to the parameter obj
            jc.setJsonToObject(process(update,"AdminConfig"), 'p');
 
        }
        public void getFlow()
        {
            WorkFlow work = WorkFlow.getWorkFlow();
            work.Token = token;
            work.Action = "getFlow";
            work.Status = 0;

            jsonConverter jc = jsonConverter.getConverter();

            string update = jc.setParameterToJson();

            jc.setJsonToObject(process(update, "AdminMonitor"), 'w');

        }
        public bool update()
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("");
                HttpWebResponse response = (HttpWebResponse) request.GetResponse();

                if (response.StatusCode == HttpStatusCode.OK)
                {
                    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                    {
                        string json = reader.ReadToEnd();
                        JObject obj = JObject.Parse(json);
                        string status = obj["status"].ToString();
                        if (status.Substring(1, status.Length - 2).Equals("fail"))
                        {
                            jsonConverter jc = jsonConverter.getConverter();
                            jc.setJsonToObject(json, 'p');
                            return false;
                        }
                        return true;
                    }

                }
                else return false;

            }
            catch (Exception ex)
            {
                throw new Exception("Communication Error : " + ex.Message, ex);
            }
            
            //make request
            //wait for response
            //if fail -> parse to parameter

           
        }
        public string process(string update, string method)
        {
           
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://gtp080:8080/StockDataServices2/" + method);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                string jsonResponse = "";
                if (HttpStatusCode.OK == response.StatusCode)
                {
                    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                    {
                        jsonResponse = reader.ReadToEnd();
                        return jsonResponse;
                    }
                } 
                else return null;
            }
            catch (Exception ex)
            {
                throw new Exception("Communication Error : " + ex.Message, ex);
            }
            

        }
        public bool testConnection()
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://gtp080:8080/StockDataServices/AdminConfig");
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                if (response == null || response.StatusCode != HttpStatusCode.OK) return false;
                return true;
            }
            catch (WebException)
            {
                return false;
            }
            
        }
    }
}
